//
//  ZYCLoginRoleView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/19.
//

#import "ZYCLoginRoleView.h"

@interface ZYCLoginRoleView ()

@property (nonatomic, strong) UIImageView *topRightImageView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *roleLabel;
@property (nonatomic, strong) UIButton *teacherButton;
@property (nonatomic, strong) UIButton *parentButton;
@property (nonatomic, strong) UIButton *comfirmButton;

@property (nonatomic, assign) ZYCLoginRoleType roleType;

@end
@implementation ZYCLoginRoleView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.topRightImageView];
        [self addSubview:self.backButton];
        [self addSubview:self.roleLabel];
        [self addSubview:self.teacherButton];
        [self addSubview:self.parentButton];
        [self addSubview:self.comfirmButton];

        [self setupConstraints];
        //默认选中教师
        self.roleType = ZYCLoginRoleTypeTeacher;
        [self clickRoleButton:self.teacherButton];
    }
    return self;
}


- (UIImageView *)topRightImageView {
    if (!_topRightImageView) {
        _topRightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bja"]];
    }
    return _topRightImageView;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"common_back"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(clickBackButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

- (UILabel *)roleLabel {
    if (!_roleLabel) {
        _roleLabel = [UILabel LabelWithString:@"选择身份" Font:kSystemBoldFont(27.0f) titleColor:HEX_RGB(kTitleDrakBlackColor) textAlignment:ZZTextAlignmentLeft];
    }
    return _roleLabel;
}

- (UIButton *)teacherButton {
    if (!_teacherButton) {
        _teacherButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_teacherButton setTitle:@"教师" forState:UIControlStateNormal];
        [_teacherButton setTitleColor:HEX_RGB(kTitleButtonColor) forState:UIControlStateNormal];
        [_teacherButton setTitleColor:HEX_RGB(@"009B4C") forState:UIControlStateSelected];
        [_teacherButton setBackgroundColor:HEX_RGB(@"F8F8F8") forState:UIControlStateNormal];
        [_teacherButton setBackgroundColor:HEX_RGB(@"E5F5ED") forState:UIControlStateSelected];
        _teacherButton.titleLabel.font = kSystemBoldFont(22.0f);
        _teacherButton.tag = 1000;
        [_teacherButton addTarget:self action:@selector(clickRoleButton:) forControlEvents:UIControlEventTouchUpInside];
        _teacherButton.layer.cornerRadius = 41.6 / 2;
        _teacherButton.layer.masksToBounds = YES;
        //去掉系统的按下高亮置灰效果
        [_teacherButton setAdjustsImageWhenHighlighted:NO];

    }
    return _teacherButton;
}

- (UIButton *)parentButton {
    if (!_parentButton) {
        _parentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_parentButton setTitle:@"家长" forState:UIControlStateNormal];
        [_parentButton setTitleColor:HEX_RGB(kTitleButtonColor) forState:UIControlStateNormal];
        [_parentButton setTitleColor:HEX_RGB(@"009B4C") forState:UIControlStateSelected];
        _parentButton.titleLabel.font = kSystemBoldFont(22.0f);
        _parentButton.tag = 1001;
//        _parentButton.selected = YES;
        [_parentButton setBackgroundColor:HEX_RGB(@"F8F8F8") forState:UIControlStateNormal];
        [_parentButton setBackgroundColor:HEX_RGB(@"E5F5ED") forState:UIControlStateSelected];
        [_parentButton addTarget:self action:@selector(clickRoleButton:) forControlEvents:UIControlEventTouchUpInside];
        _parentButton.layer.cornerRadius = 41.6 / 2;
        _parentButton.layer.masksToBounds = YES;
        //去掉系统的按下高亮置灰效果
        [_parentButton setAdjustsImageWhenHighlighted:NO];
//        _parentButton.layer.borderWidth = 2.0f;
//        _parentButton.layer.borderColor = HEX_RGB(@"009B4C").CGColor;
    }
    return _parentButton;
}

- (UIButton *)comfirmButton {
    if (!_comfirmButton) {
        _comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _comfirmButton.frame = CGRectMake(0,0, kScreenWidth - 2 * 32, 48);
        [_comfirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_comfirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _comfirmButton.titleLabel.font = kSystemBoldFont(22.0f);
        _comfirmButton.backgroundColor = HEX_RGB(@"#52E1E3");
        [_comfirmButton addTarget:self action:@selector(clickComfirmAction:) forControlEvents:UIControlEventTouchUpInside];
        _comfirmButton.layer.cornerRadius = 24;
        _comfirmButton.layer.masksToBounds = YES;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#52E1E3").CGColor,
                                 (__bridge id)HEX_RGB(@"#8BDFBC").CGColor];
        gradientLayer.locations = @[@(0.0),@(0.4f)];
        gradientLayer.frame = _comfirmButton.bounds;
        gradientLayer.cornerRadius = 24;
        [_comfirmButton.layer insertSublayer:gradientLayer atIndex:0];
    }
    return _comfirmButton;
}


#pragma mark - 点击事件
- (void)clickComfirmAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(loginRoleViewDidRoleComfirm:)]) {
        [self.delegate loginRoleViewDidRoleComfirm:self.roleType];
    }
}

- (void)clickRoleButton:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.tag == 1000) {//教师
        if (sender.selected) {
            self.parentButton.selected = NO;
            self.parentButton.layer.borderColor = HEX_RGB(@"F8F8F8").CGColor;
            sender.layer.borderWidth = 2.0f;
            sender.layer.borderColor = HEX_RGB(@"009B4C").CGColor;
            self.roleType = ZYCLoginRoleTypeTeacher;
        }

    }else{// 家长
        if (sender.selected) {
            self.teacherButton.selected = NO;
            self.teacherButton.layer.borderColor = HEX_RGB(@"F8F8F8").CGColor;
            sender.layer.borderWidth = 2.0f;
            sender.layer.borderColor = HEX_RGB(@"009B4C").CGColor;
            self.roleType = ZYCLoginRoleTypeParent;
        }
    }

    
}
- (void)clickBackButton:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(loginRoleViewDidBack)]) {
        [self.delegate loginRoleViewDidBack];
    }
}


- (void)setupConstraints{
    [self.topRightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(142, 142));
    }];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32.0f);
        make.top.equalTo(self).offset(51.0f);
    }];
    
    [self.roleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32.0f);
        make.top.equalTo(self).offset(110.0f);
    }];


    [self.teacherButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.roleLabel.mas_bottom).offset(54.0f);
        make.size.mas_equalTo(CGSizeMake(310, 128.0f));
    }];
    
    [self.parentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.teacherButton.mas_bottom).offset(19.0f);
        make.size.mas_equalTo(CGSizeMake(310, 128.0f));
    }];
    
    [self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(32.0f);
        make.right.equalTo(self).offset(-32.0f);
        make.top.equalTo(self.parentButton.mas_bottom).offset(81.0f);
        make.height.mas_equalTo(48.0f);
    }];

}

@end
